package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.Episode;
import com.le123.ysdq.R;

import java.util.ArrayList;

public class EpisodeListAdapter extends RecyclerView.Adapter<EpisodeListAdapter.ViewHolder> {

	private Context mContext;
	private LayoutInflater inflater;
	private ArrayList<Episode> episodes;
	private int mCurrentPlayPosition;
	private EpisodeGridAdapter.OnItemClickListener mListener;

	public static class ViewHolder extends RecyclerView.ViewHolder
	{
		public ViewHolder(View arg0)
		{
			super(arg0);
		}
		TextView tv;
		ImageView ispay;
	}

	public EpisodeListAdapter(Context mContext, ArrayList<Episode> episodes) {
		super();
		this.mContext = mContext;
		this.episodes = episodes;
		this.inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
	{
		View view = inflater.inflate(R.layout.episode_list_item,
				viewGroup, false);
		ViewHolder viewHolder = new ViewHolder(view);

		viewHolder.tv = (TextView) view
				.findViewById(R.id.episode_list_item_text);
		viewHolder.ispay = (ImageView) view
				.findViewById(R.id.ispay);
		return viewHolder;
	}

	@Override
	public int getItemCount() {
		return episodes.size();
	}

	@Override
	public void onBindViewHolder(final ViewHolder viewHolder, final int i)
	{
		viewHolder.tv.setText(episodes.get(i).getPorder());
		if (mCurrentPlayPosition == i) {
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_3599f8));
		} else {
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_ff444444));
		}

		if (!TextUtils.isEmpty(episodes.get(i).getPorder())) {
			viewHolder.tv.setText("第" + episodes.get(i).getPorder() + "期");
		}
		if (!TextUtils.isEmpty(episodes.get(i).getSubName())) {
			viewHolder.tv.setText(episodes.get(i).getSubName());
		}
		if (mCurrentPlayPosition == i) {
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_3599f8));
			viewHolder.itemView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.rectangle_round_corner_3_background));
		} else {
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_666666));
			viewHolder.itemView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.rectangle_round_corner_3_no_stroke));
		}

		if ("1".equals(episodes.get(i).getIsPay())){
			viewHolder.ispay.setVisibility(View.VISIBLE);
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_bebebe));
		}else{
			viewHolder.ispay.setVisibility(View.GONE);
		}

		if (mListener != null)
		{
			viewHolder.tv.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v) {
					if (i < episodes.size()) {
						mListener.onItemClick(viewHolder.itemView, i, episodes.get(i).getPorder());
					}
				}
			});

		}

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setEpisodes(ArrayList<Episode> episodes) {
		this.episodes=episodes;
		notifyDataSetChanged();
	}

	public ArrayList<Episode> getEpisodes() {
		return episodes;
	}

	public void setSelectPosition(int position) {
		if (position == mCurrentPlayPosition) {
			return;
		}
		mCurrentPlayPosition = position;
		notifyDataSetChanged();
	}

	public void setOnItemClickListener(EpisodeGridAdapter.OnItemClickListener onItemClickListener) {
		mListener = onItemClickListener;
	}

}
